import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class OpenFileGUI extends JPanel{
	JButton file  = new JButton("打开文件");
	//将指向被选中的文件
	CardLayout  layoutMain;//从主面板那里拿到的布局管理器
	JPanel mainPanel;//主面板
	IOGUI IO_Card;
	
	public OpenFileGUI(CardLayout  layoutMain,JPanel mainPanel,IOGUI IO_Card) {
		//初始化mainPanel和layoutMain和IOGUI
		this.mainPanel = mainPanel;
		this.layoutMain = layoutMain;
		this.IO_Card = IO_Card;
		
		FlowLayout fl = new FlowLayout(FlowLayout.CENTER);
		fl.setVgap(200);
		setLayout(fl);
		add(file);
		//为"打开文件"button添加一个鼠标点击事件, 由内部类监听执行
		file.addMouseListener(new OpenFileGUIEventListener());
	}
	//监听执行的 内部类
	private class OpenFileGUIEventListener extends MouseAdapter{
		public void mouseClicked(MouseEvent event) {
			//为文件选择器提供一个当前(默认)目录
			File filepath = new File("C:\\Users\\胡伟\\Desktop\\Java-WorkSpace\\java-example-3\\src\\main\\resource");
			JFileChooser fileChooser = new JFileChooser(filepath);
			//打开文件选择器的对话框
			fileChooser.showOpenDialog(null);
			IO_Card.setFilePath(fileChooser.getSelectedFile());
			if(fileChooser.getSelectedFile()==null) {
				System.err.println("打开文件异常!");
			}
			layoutMain.show(mainPanel, "IO Card");
		}
	}
}